﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class StatusReg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["user"] != null)
            {
                this.Log_in1.Visible = false;
                this.Log_out1.Visible = true;
            }
            else
            {
                Response.Redirect("Login.aspx");
            }

            this.btnSave.Visible = false;
            this.btnAdd.Enabled = true;
            //load BusCataloge List
            this.BindGrid();
        }
        else
        {
        }
    }
    public void BindGrid()
    {
        //load BusCataloge List
        ProStatus pstt = new ProStatus();
        DataSet ds = pstt.LoadStatus();

        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            this.grdListStatus.DataSource = ds.Tables[0];
            this.grdListStatus.DataBind();
            this.grdListStatus.Rows[0].Visible = false;
        }
        else
        {
            this.grdListStatus.DataSource = ds.Tables[0];
            this.grdListStatus.DataBind();
            this.grdListStatus.RowStyle.HorizontalAlign = HorizontalAlign.Center;
            this.grdListStatus.HeaderRow.Cells[0].Width = 20;
            this.grdListStatus.HeaderRow.Cells[1].Width = 20;
            this.grdListStatus.HeaderRow.Cells[2].Width = 30;
            this.grdListStatus.HeaderRow.Cells[2].Text = "ID";
            this.grdListStatus.HeaderRow.Cells[3].Text = "Name of Status";
           
            this.grdListStatus.HeaderRow.Cells[3].Width = 200;
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ProStatus pstt = new ProStatus();
        if (pstt.CheckStatusExist(this.txtNameofStatus.Text) == true)
        {
            this.lblError.Text = Constant.MESSAGE_ERROR_NAMEBUSCAT_EXIST;
            this.txtNameofStatus.Text = "";
            return;
        }
        if (this.txtNameofStatus.Text.Trim() == null || this.txtNameofStatus.Text.Trim() == "")
        {
            this.lblError.Text = "Please input name of Status";
            return;
        }

        Status stt = new Status();
        stt.StrStatus = this.txtNameofStatus.Text;
        Boolean bResult = pstt.SaveStatus(stt);
        if (bResult == true)
        {
            this.BindGrid();
            this.txtNameofStatus.Text = "";
            this.lblError.Text = Constant.MESSAGE_INSERT_SUCCESS;
        }
        else
        {
            this.lblError.Text = Constant.MESSAGE_INSERT_ERROR;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //Update to DB
        ProStatus pstt = new ProStatus();
        Status stt = new Status();
        stt.Id = Int32.Parse(this.grdListStatus.SelectedRow.Cells[2].Text);
        stt.StrStatus= this.txtNameofStatus.Text;
        
        if (this.txtNameofStatus.Text.Trim() == null || this.txtNameofStatus.Text.Trim() == "")
        {
            this.lblError.Text = "Please input name of Status";
            return;
        }
        Boolean bResult = pstt.UpdateStatus(stt);
        if (bResult == true)
        {
            this.txtNameofStatus.Text = "";
            this.btnSave.Visible = false;
            this.lblError.Text = Constant.MESSAGE_UPDATE_SUCCESS;
            BindGrid();
            this.btnAdd.Visible = true;
        }
        else
        {
            this.lblError.Text = Constant.MESSAGE_UPDATE_ERROR;
        }
    }
    protected void grdListStatus_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        this.grdListStatus.SelectedIndex = Int32.Parse(e.CommandArgument.ToString());
        this.lblError.Text = "";
    }
    protected void grdListStatus_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ProStatus pstt = new ProStatus();
        Status stt = new Status();
        stt.Id = Int32.Parse(this.grdListStatus.SelectedRow.Cells[2].Text);
        Boolean bResult = pstt.DeleteStatus(stt);
        if (bResult == true)
        {
            this.BindGrid();
            this.lblError.Text = Constant.MESSAGE_DELETE_SUCCESS;
        }
        else
        {
            this.lblError.Text = Constant.MESSAGE_DELETE_ERROR;
        }
    }
    protected void grdListStatus_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Status stt = new Status();
        stt.Id = Int32.Parse(this.grdListStatus.SelectedRow.Cells[2].Text);
        stt.StrStatus = this.grdListStatus.SelectedRow.Cells[3].Text;
        this.txtNameofStatus.Text = stt.StrStatus.ToString();
        this.btnSave.Visible = true;
        this.btnAdd.Visible = false;
    }
}
